package com.ibm.moa.outmail.view;

import com.ibm.moa.outmail.utils.DensityUtil;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ExpandableListView;

public class ExpandableListViewWithIntercept extends ExpandableListView {

	private float mLastMotionX;
	private float mLastMotionY;
	private Context mContext;

	public ExpandableListViewWithIntercept(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
	}

	public ExpandableListViewWithIntercept(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
	}

	public ExpandableListViewWithIntercept(Context context) {
		super(context);
		this.mContext = context;
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {

		final float x = ev.getX();
		final float y = ev.getY();

		switch (ev.getAction()) {
		case MotionEvent.ACTION_MOVE:
			final int xDiff = (int) Math.abs(x - mLastMotionX);
			final int yDiff = (int) Math.abs(y - mLastMotionY);
			if (Math.abs(yDiff) < DensityUtil.dip2px(mContext, 5) ||  xDiff > yDiff) {
				return false;
			}
			break;

		case MotionEvent.ACTION_DOWN:
			mLastMotionX = x;
			mLastMotionY = y;
			break;
		}

		return super.onInterceptTouchEvent(ev);

	}
}
